package com.zt.ztyh.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zt.ztyh.dto.FundsCount;
import com.zt.ztyh.dto.FundsDto;
import com.zt.ztyh.dto.FundsUserDto;
import com.zt.ztyh.entity.Funds;
import com.zt.ztyh.entity.Fundsinfo;

import com.zt.ztyh.service.FundsService;

import com.zt.ztyh.vo.common.Result;
import com.zt.ztyh.vo.common.ResultCodeEnum;
import com.zt.ztyh.vo.funds.FundsUserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/ztyh/funds")
@Tag(name = "理财模块")
public class FundsController {


    @Autowired
    private FundsService fundsService;

    /**
     * 理财产品列表
     * @param page
     * @param limit
     * @param fundsDto
     * @param
     * @return
     */
    @GetMapping("/findFunds/{page}/{limit}")
    @Operation(summary = "理财产品列表")
    public Result<Page<Funds>> findAllFunds(@PathVariable("page") Integer page,
                                            @PathVariable("limit") Integer limit,
                                            FundsDto fundsDto

    ) {
        return Result.build(fundsService.findFunds(page, limit,fundsDto), ResultCodeEnum.SUCCESS);
    }


    /**
     * 理财产品主键详情
     * @param fid
     * @return
     */
    @GetMapping("/findFundsInfo/{fid}")
    @Operation(summary = "理财产品详情接口")
    public Result<List<Fundsinfo>> findFundsInfo(@PathVariable("fid") Long fid) {
//        Long aLong = Long.valueOf(fid);
//        System.out.println("fid===>"+fid);
//        System.out.println("along==>"+aLong);
        return Result.build(fundsService.findFundsInfo(fid), ResultCodeEnum.SUCCESS);
    }

    @PostMapping("/buyFunds")
    @Operation(summary = "购买理财产品接口")
    public Result buyFunds(@RequestHeader(name = "token")String token,@RequestBody FundsDto fundsDto) {
        fundsService.buyFunds(token,fundsDto);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询用户理财
     * @param
     * @return
     */
    @GetMapping("/findByUser/{page}/{limit}")
    @Operation(summary = "查询用户的理财产品")
    public Result<Page<FundsUserVo>> findByUser(@PathVariable("page") Integer page,@PathVariable("limit") Integer limit,
                                                FundsDto fundsDto) throws ParseException {
        System.out.println("hahha"+fundsDto);
        //算理财收益
        Page<FundsUserVo> byUser = fundsService.findByUser(page,limit,fundsDto);
        return Result.build(byUser,ResultCodeEnum.SUCCESS);
    }


    /**
     * SELECT fid,count(uid) from ztyh_funds_user where state = 0 GROUP BY fid
     * 查询该理财产品的用户量
     * @param fundsId
     * @return
     */
    @GetMapping("/findByFunds")
    @Operation( summary = "查询该理财产品的用户量")
    public List<FundsCount> findByFunds(Long fundsId){
        return fundsService.findByFunds(fundsId);
    }

    /**
     * 赎回
     * @param fundsUserDto
     * @return
     */
    @PutMapping("/updateFunds")
    @Operation(summary = "产品赎回")
    public Result updateFunds(@RequestHeader(name = "token")String token,@RequestBody FundsUserDto fundsUserDto) throws ParseException {
        System.out.println("====>"+fundsUserDto);
       fundsService.updateFunds(token,fundsUserDto);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }






}
